package com.yexiao.cofe.business.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.yexiao.cofe.business.mapper.CategoryMapper;
import com.yexiao.cofe.business.pojo.Category;
import com.yexiao.cofe.business.service.CategoryService;
import com.yexiao.cofe.common.exception.BusinessException;
import com.yexiao.core.util.JSONUtil;
import com.yexiao.core.util.StrUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public void addCategory(JSONObject param) {
        String name = JSONUtil.checkAndGetStr(param, "name");
        Integer parentId = JSONUtil.checkAndGetInt(param, "parentId");

        // 查询有无重名
        Category category = categoryMapper.selectByName(name);
        if(category != null){
            throw new BusinessException("类别名重复");
        }

        // 查看上级是否存在
        Category parent = categoryMapper.selectById(parentId);
        if(parent == null){
            throw new BusinessException("父节点不存在");
        }

        Category newCategory = new Category();
        newCategory.setName(name);
        newCategory.setParentId(parentId);
        newCategory.setLevel(parent.getLevel() + 1);
        categoryMapper.insert(newCategory);
    }

    @Override
    public List<JSONObject> listAll() {
        List<Category> categoryList = categoryMapper.listAll();
        categoryList.sort((a,b) -> a.getSort() - b.getSort());

        // 默认以0为顶级节点
        return getCategoryTree(categoryList, -1);
    }

    @Override
    public void deleteCategory(JSONObject param) {
        Integer id = JSONUtil.checkAndGetInt(param, "id");
        if(id == 1) {
            throw new BusinessException("无法删除根节点");
        }

        int childSize = categoryMapper.getChildSize(id);
        if(childSize > 0){
            throw new BusinessException("存在子节点，无法删除");
        }

        categoryMapper.deleteById(id);
    }

    @Override
    public void modifyCategory(JSONObject param) {
        Integer id = JSONUtil.checkAndGetInt(param, "id");
        String name = JSONUtil.getStr(param, "name");
        Integer parentId = JSONUtil.getInt(param, "parentId");
        Integer sort = JSONUtil.getInt(param, "sort");

        // 获取要修改的节点
        Category category = categoryMapper.selectById(id);
        if(category == null){
            throw new BusinessException("节点不存在");
        }

        // 不传父节点，默认为顶级节点
        if(parentId != null) {
            Category parent = categoryMapper.selectById(parentId);
            if(parent == null) {
                throw new BusinessException("父节点不存在");
            }

            category.setParentId(parentId);
            category.setLevel(parent.getLevel() + 1);
        }

        if(sort != null) {
            category.setSort(sort);
        }
        if(StrUtil.isNotEmpty(name)) {
            category.setName(name);
        }

        categoryMapper.updateById(category);
    }

    private List<JSONObject> getCategoryTree(List<Category> list, Integer parentId) {

        List<JSONObject> child = new ArrayList<>();
        for (Category category : list) {
            if(category.getParentId().equals(parentId)) {
                JSONObject msg = new JSONObject();
                msg.putAll(getCategoryBaseMsg(category));
                msg.put("child", getCategoryTree(list, category.getId()));
                child.add(msg);
            }
        }

        return child;
    }

    private JSONObject getCategoryBaseMsg(Category category) {
        return JSONUtil.of(
                "name", category.getName(),
                "id", category.getId(),
                "sort" , category.getSort()
        );
    }


}
